package dao;

import bean.LeaveReturn;
import util.DriverManegerUitl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

public class LeaveReturnDao {
    private Connection conn;
    private PreparedStatement pre;
    private ResultSet resultSet;

    /**
     *学生，管理员提交
     * 提交
     * @param leaveReturn
     */
    //学生提交离开返回时间
    public void submit(LeaveReturn leaveReturn) {
        conn = DriverManegerUitl.getConnection();
        long leavetime = leaveReturn.getLeavetime().getTime();
        long returntime=leaveReturn.getReturntime().getTime();

        try {
            pre=conn.prepareStatement("insert into leavereturn (studentname,dormitoryid,leavetime,returntime) value (?,?,?,?)");
            pre.setString(1,leaveReturn.getStudentname());
            pre.setString(2,leaveReturn.getDormitoryid());
            pre.setDate(3,new java.sql.Date(leavetime));
            pre.setDate(4,new java.sql.Date(returntime));
            pre.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DriverManegerUitl.closed(conn,pre,resultSet);
        }
    }

    //根据学生姓名查询提交过后的信息

    /**
     * 根据学生姓名查询
     * @param studentname
     * @return
     */
    public ArrayList<LeaveReturn> getLeaveReturnByStudentname(String studentname){
        ArrayList<LeaveReturn> studentList=new ArrayList<>();
        conn=DriverManegerUitl.getConnection();
        try {
            pre=conn.prepareStatement("select * from leavereturn where studentname=?");
            pre.setString(1,studentname);
            resultSet= pre.executeQuery();
            while (resultSet.next()){
                LeaveReturn leaveReturn=new LeaveReturn();
                leaveReturn.setStudentname(resultSet.getString("studentname"));
                leaveReturn.setDormitoryid(resultSet.getString("dormitoryid"));
                leaveReturn.setLeavetime(resultSet.getDate("leavetime"));
                leaveReturn.setReturntime(resultSet.getDate("returntime"));
                studentList.add(leaveReturn);
            }
            return studentList;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DriverManegerUitl.closed(conn,pre,resultSet);
        }
        return null;
    }

    /**
     * 管理员端
     * @return
     */
    //查询表中所有信息
    public ArrayList<LeaveReturn> getAllLeavareturn(){
        conn=DriverManegerUitl.getConnection();
        ArrayList<LeaveReturn> list=new ArrayList<>();
        try {
            pre=conn.prepareStatement("select * from leavereturn");
            resultSet= pre.executeQuery();
            while (resultSet.next()){
                LeaveReturn leaveReturn=new LeaveReturn();
                leaveReturn.setStudentname(resultSet.getString("studentname"));
                leaveReturn.setDormitoryid(resultSet.getString("dormitoryid"));
                leaveReturn.setLeavetime(resultSet.getDate("leavetime"));
                leaveReturn.setReturntime(resultSet.getDate("returntime"));
                list.add(leaveReturn);
            }
            return list;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DriverManegerUitl.closed(conn,pre,resultSet);
        }
        return null;
    }

    /**
     * 管理员端修改
     * @param leaveReturn
     */
//根据宿舍号，姓名修改信息
    public void changeTime(LeaveReturn leaveReturn){
        conn=DriverManegerUitl.getConnection();
        try {
            pre=conn.prepareStatement("update leavereturn set  leavetime=?,returntime=? where studentname=? and dormitoryid=?");
            pre.setDate(1,new java.sql.Date(leaveReturn.getLeavetime().getTime()));
            pre.setDate(2,new java.sql.Date(leaveReturn.getReturntime().getTime()));
            pre.setString(3,leaveReturn.getStudentname());
            pre.setString(4,leaveReturn.getDormitoryid());
            pre.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DriverManegerUitl.closed(conn,pre,resultSet);
        }
    }

    //管理员删除
//    根据用户名删除信息
public  void delInfoBySutdentname(String name){
    conn=DriverManegerUitl.getConnection();
    try {
        pre=conn.prepareStatement("delete from leavereturn where studentname=?");
        pre.setString(1,name);
        pre.executeUpdate();
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    }finally {
        DriverManegerUitl.closed(conn,pre,resultSet);
    }
}
}
